package com.testmycat.zbf.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.testmycat.zbf.dao.TestMycatMapper;
import com.testmycat.zbf.entity.UserInfo;
import com.testmycat.zbf.utils.RandomAddressUtils;
import com.testmycat.zbf.utils.RandomNameUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author: LCG
 * @date: 2022-05-12 17:18:51
 * @description:
 **/
@RestController
@Slf4j
public class TestMycatController {

    @Autowired
    private TestMycatMapper testMycatMapper;

    @RequestMapping("test01")
    private Object test01(){
       return testMycatMapper.selectById(1);
    }
    @RequestMapping("test03")
    public void test03(){

        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                test02();
            }
        });

        thread.start();
    }


    public void test02(){
        List<UserInfo> list=new ArrayList<>();
        UserInfo userInfo=null;
        List<String> listFriends=new ArrayList<>();
        listFriends.add( RandomNameUtils.getRandomName());
        listFriends.add( RandomNameUtils.getRandomName());
       for (int i=0;i<5000;i++){
           if(i!=0&&i%3000==0){
               listFriends.clear();
               listFriends.add( RandomNameUtils.getRandomName());
               listFriends.add( RandomNameUtils.getRandomName());
               testMycatMapper.batchInsert(list);
               list.clear();
               log.info("======执行的多少=====>"+i);
           }else{
               String randomName = RandomNameUtils.getRandomName();
               String randomAddress = RandomAddressUtils.getRandomAddress();
               userInfo=new UserInfo();
               userInfo.setRoleName(randomName);
               userInfo.setAddress(randomAddress);
               userInfo.setAge(new Random().nextInt(100));
               userInfo.setDateTime(new Date());
               userInfo.setDouyin("DOUYIN："+randomName);
               userInfo.setFriends(listFriends);
               userInfo.setUserInfo(randomAddress);
               userInfo.setUserName(randomName);
               userInfo.setWeiXin("WEIXIN："+randomName);
               list.add(userInfo);
            }
       }

        if(list.size()>0){
            testMycatMapper.batchInsert(list);
            list.clear();
        }

    }


    @RequestMapping("test04/{pageSize}/{pageNum}")
    public Object getNextPage(@PathVariable("pageSize") Integer pageSize,
                              @PathVariable("pageNum") Integer pageNum){

        Page<UserInfo> page=new Page<>(pageNum,pageSize);


        return testMycatMapper.getPageLIst(page);
    }

    @RequestMapping("test05")
    public Object insertOnedata(){
        List<UserInfo> list=new ArrayList<>();
        UserInfo userInfo=null;
        List<String> listFriends=new ArrayList<>();
        listFriends.add( RandomNameUtils.getRandomName());
        listFriends.add( RandomNameUtils.getRandomName());
        String randomName = RandomNameUtils.getRandomName();
        String randomAddress = RandomAddressUtils.getRandomAddress();
        userInfo=new UserInfo();
        userInfo.setRoleName(randomName);
        userInfo.setAddress(randomAddress);
        userInfo.setAge(new Random().nextInt(100));
        userInfo.setDateTime(new Date());
        userInfo.setDouyin("DOUYIN："+randomName);
        userInfo.setFriends(listFriends);
        userInfo.setUserInfo(randomAddress);
        userInfo.setUserName(randomName);
        userInfo.setWeiXin("WEIXIN："+randomName);
        list.add(userInfo);

        return  testMycatMapper.batchInsert(list);

    }

}
